import { ref } from 'vue'
import { ElMessage } from 'element-plus'

export default function useCRUD(options: any) {
  // 查询相关 ---------------------------------------------------------------
  const params = ref(getDefaultParams())

  const list = ref([])

  const total = ref<number>(0)

  const loading = ref<boolean>(false)

  async function handleQuery(): Promise<void> {
    const { queryApi } = options
    loading.value = true
    try {
      const res = await queryApi(params.value)
      loading.value = false
      list.value = 'list' in res.data ? res.data.list : res.data
      total.value = 'total' in res.data ? res.data.total : res.data.length
    } catch {
      loading.value = false
    }
  }

  async function handleReset(): Promise<void> {
    params.value = getDefaultParams()
    handleQuery()
  }

  function getDefaultParams() {
    const { newParams } = options
    return Object.assign({ page: 1, limit: 10 }, newParams)
  }

  // 新增/编辑相关 -----------------------------------------------------------------
  async function handleCreate(data: any) {
    const { createApi } = options
    if (!createApi) return

    loading.value = true
    try {
      const { message } = await createApi(data)
      dialogVisible.value = false
      loading.value = false
      ElMessage({ type: 'success', message })
      handleQuery()
    } catch {
      loading.value = false
      ElMessage({ type: 'error', message: '操作失败' })
    }
  }

  const formData = ref({})
  async function getOne(id: number | string): Promise<void> {
    const { getOneApi } = options
    if (!getOneApi) return

    const res = await getOneApi(id)
    formData.value = res.data
  }

  const dialogVisible = ref<boolean>(false)
  const dialogTitle = ref<string>('')
  function openDialog(id?: number | string): void {
    dialogTitle.value = id ? '编辑' : '新增'
    if (id) {
      getOne(id)
    }
    dialogVisible.value = true
  }

  async function handleUpdate(id: any, data: any) {
    const { updateApi } = options
    if (!updateApi) return

    loading.value = true
    try {
      const { message } = await updateApi(id, data)
      dialogVisible.value = false
      loading.value = false
      ElMessage({ type: 'success', message })
      handleQuery()
    } catch {
      loading.value = false
      ElMessage({ type: 'error', message: '操作失败' })
    }
  }

  function handleConfirm(data: any) {
    if (data.id) {
      handleUpdate(data.id, data)
    } else {
      handleCreate(data)
    }
  }

  // 删除相关 -----------------------------------------------------------------
  async function handleDelete(id: any) {
    const { deleteApi } = options
    if (!deleteApi) return

    loading.value = true
    try {
      const res = await deleteApi(id)
      loading.value = false
      handleQuery()
      return res
    } catch {
      loading.value = false
    }
  }

  return {
    params,
    handleQuery,
    list,
    loading,
    handleReset,
    handleCreate,
    openDialog,
    dialogVisible,
    handleDelete,
    formData,
    handleUpdate,
    dialogTitle,
    total,
    handleConfirm
  }
}
